<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/db.php');
class film
{
	private $db;
	private $table_name = 'film';
	public static $is_copyright_expired = array(
		'yes' => 1,
		'no' => 2,
	);
	function __construct()
	{
		$this->db = new db();
	}
	public function insert($data)
	{
		if(isset($data['cast'])&&!empty($data['cast']))
		{
			$data_for_query['cast'] = trim($data['cast']);
		}
		if(isset($data['cinematographer'])&&!empty($data['cinematographer']))
		{
			$data_for_query['cinematographer'] = trim($data['cinematographer']);
		}
		if(isset($data['copyright_id'])&&!empty($data['copyright_id']))
		{
			$data_for_query['copyright_id'] = intval($data['copyright_id']);
		}
		if(isset($data['copyright_began_time'])&&!empty($data['copyright_began_time']))
		{
			$data_for_query['copyright_began_time'] = trim($data['copyright_began_time']);
		}
		if(isset($data['country_id'])&&!empty($data['country_id']))
		{
			$data_for_query['country_id'] = intval($data['country_id']);
		}
		if(isset($data['director'])&&!empty($data['director']))
		{
			$data_for_query['director'] = trim($data['director']);
		}
		if(isset($data['douban_id'])&&!empty($data['douban_id']))
		{
			$data_for_query['douban_id'] = intval($data['douban_id']);
		}
		if(isset($data['imdb_id'])&&!empty($data['imdb_id']))
		{
			$data_for_query['imdb_id'] = trim($data['imdb_id']);
		}
		if(isset($data['is_copyright_expired'])&&!empty($data['is_copyright_expired']))
		{
			$data_for_query['is_copyright_expired'] = intval($data['is_copyright_expired']);
		}
		if(isset($data['spoken_language_id'])&&!empty($data['spoken_language_id']))
		{
			$data_for_query['spoken_language_id'] = intval($data['spoken_language_id']);
		}
		if(isset($data['synopsis'])&&!empty($data['synopsis']))
		{
			$data_for_query['synopsis'] = trim($data['synopsis']);
		}
		if(isset($data['screenwriter'])&&!empty($data['screenwriter']))
		{
			$data_for_query['screenwriter'] = trim($data['screenwriter']);
		}
		$sql = "INSERT INTO `".$this->table_name."` (".$this->db->implodeToColumn(array_keys($data_for_query)).") VALUES (".$this->db->implodeToValues(array_values($data_for_query)).")";
		$this->db->query($sql);
		$id = $this->db->lastInsertId();
		if(empty($id))
		{
			//throw new Exception("INSERT INTO `".$this->table_name."` faild");
			throw new Exception('');
		}
		else
		{
			return $id;
		}
	}
	
	public function select($where,$column='*')
	{
		$sql_1 = "SELECT ".$this->db->implodeToColumn($column)." FROM ".$this->table_name." WHERE ".$this->db->implodeToWhere($where);
		$result_1 = $this->db->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$fetch_result = $result_1->fetchAll();
		if($fetch_result==false)
		{
			//throw new Exception("faild");
			throw new Exception("");
		}
		else
		{
			return $fetch_result[0];
		}
	}
	
	public function update($where,$data)
	{
		if(isset($data['cast'])&&!empty($data['cast']))
		{
			$data_for_query['cast'] = trim($data['cast']);
		}
		if(isset($data['cinematographer'])&&!empty($data['cinematographer']))
		{
			$data_for_query['cinematographer'] = trim($data['cinematographer']);
		}
		if(isset($data['copyright_id'])&&!empty($data['copyright_id']))
		{
			$data_for_query['copyright_id'] = intval($data['copyright_id']);
		}
		if(isset($data['copyright_began_time'])&&!empty($data['copyright_began_time']))
		{
			$data_for_query['copyright_began_time'] = trim($data['copyright_began_time']);
		}
		if(isset($data['country_id'])&&!empty($data['country_id']))
		{
			$data_for_query['country_id'] = intval($data['country_id']);
		}
		if(isset($data['director'])&&!empty($data['director']))
		{
			$data_for_query['director'] = trim($data['director']);
		}
		if(isset($data['douban_id'])&&!empty($data['douban_id']))
		{
			$data_for_query['douban_id'] = intval($data['douban_id']);
		}
		if(isset($data['imdb_id'])&&!empty($data['imdb_id']))
		{
			$data_for_query['imdb_id'] = trim($data['imdb_id']);
		}
		if(isset($data['is_copyright_expired'])&&!empty($data['is_copyright_expired']))
		{
			$data_for_query['is_copyright_expired'] = intval($data['is_copyright_expired']);
		}
		if(isset($data['spoken_language_id'])&&!empty($data['spoken_language_id']))
		{
			$data_for_query['spoken_language_id'] = intval($data['spoken_language_id']);
		}
		if(isset($data['synopsis'])&&!empty($data['synopsis']))
		{
			$data_for_query['synopsis'] = trim($data['synopsis']);
		}
		if(isset($data['screenwriter'])&&!empty($data['screenwriter']))
		{
			$data_for_query['screenwriter'] = trim($data['screenwriter']);
		}
		$sql = "UPDATE `".$this->table_name."` SET ".$this->db->implodeToUpdate($data_for_query)." WHERE ".$this->db->implodeToWhere($where);
		$count = $this->db->query($sql);
		return $count;
	}
	
	public function updateById($id,$data)
	{
		$where = array(
			'id' => $id,
		);
		return $this->update($where,$data);
	}
}

?>
